package com.lzb.play.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.GsonUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.reflect.TypeToken;
import com.lzb.play.BaseNoModelFragment;
import com.lzb.play.R;
import com.lzb.play.activity.MyActivity;
import com.lzb.play.adapter.HomeBirdAdapter;
import com.lzb.play.adapter.MyBannerAdapter;
import com.lzb.play.bean.BirdBean;
import com.lzb.play.bean.HomeBirdBean;
import com.lzb.play.databinding.FragmentHomeBinding;
import com.lzb.play.dialog.BirdInfoDialog;
import com.lzb.play.dialog.VolumeSetDialog;
import com.youth.banner.indicator.RectangleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> implements View.OnClickListener, OnItemClickListener {

    private HomeBirdAdapter homeBirdAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        mDataBinding.ivVolume.setOnClickListener(this);
        mDataBinding.ivMy.setOnClickListener(this);

        mDataBinding.rvBirdList.setLayoutManager(new GridLayoutManager(mContext, 3));
        homeBirdAdapter = new HomeBirdAdapter();
        mDataBinding.rvBirdList.setAdapter(homeBirdAdapter);
        homeBirdAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivVolume) {//声音设置
            showVolumeSetDialog();
        } else if (id == R.id.ivMy) {//个人中心
            startActivity(MyActivity.class);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        showBirdInfo(homeBirdAdapter.getItem(position));
    }

    @Override
    protected void initData() {
        getData();
    }

    // 读取JSON文件
    public String loadJSONFromAsset(Context context) {
        String json = null;
        try {
            InputStream is = context.getAssets().open("bird_species.json");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            json = new String(buffer, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return json;
    }

    private void getData() {
        String json = loadJSONFromAsset(mContext);
        List<BirdBean> listBird = GsonUtils.fromJson(json, new TypeToken<List<BirdBean>>() {
        }.getType());

        List<HomeBirdBean> listData = new ArrayList<>();
        HomeBirdBean homeBirdBean = new HomeBirdBean(R.drawable.maque, R.drawable.maque2, R.raw.maque, listBird.get(0));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.bailu, R.drawable.bailu2, R.raw.bailu, listBird.get(1));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.laoying, R.drawable.laoying2, R.raw.laoying, listBird.get(2));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.zhuomuniao, R.drawable.zhuomuniao2, R.raw.zhuomuniao, listBird.get(3));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.gezi, R.drawable.gezi2, R.raw.gezi, listBird.get(4));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.yingwu, R.drawable.yingwu2, R.raw.yingwu, listBird.get(5));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.fengniao, R.drawable.fengniao2, R.raw.fengniao, listBird.get(6));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.kongque, R.drawable.kongque2, R.raw.kongque, listBird.get(7));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.maaotouying, R.drawable.maotouying2, R.raw.maotouying, listBird.get(8));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.bailingniao, R.drawable.bailingniao2, R.raw.bailingniao, listBird.get(9));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.huangli, R.drawable.huangli2, R.raw.huangli, listBird.get(10));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.huangque, R.drawable.huangque2, R.raw.huangque, listBird.get(11));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.jiayan, R.drawable.jiayan2, R.raw.jiayan, listBird.get(12));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.haiou, R.drawable.haiou2, R.raw.haiou, listBird.get(13));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.liuying, R.drawable.liuying2, R.raw.liuying, listBird.get(14));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.cuiniao, R.drawable.cuiniao2, R.raw.cuiniao, listBird.get(15));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.quejiu, R.drawable.quejiu2, R.raw.quequ, listBird.get(16));
        listData.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.shanbanjiu, R.drawable.shanbanjiu2, R.raw.shanbanjiu, listBird.get(17));
        listData.add(homeBirdBean);
        homeBirdAdapter.setList(listData);

        List<HomeBirdBean> listBanner = new ArrayList<>();
        homeBirdBean = new HomeBirdBean(R.drawable.xique, R.drawable.xique2, R.raw.xique, listBird.get(18));
        listBanner.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.haiou4, R.drawable.haiou2, R.raw.haiou, listBird.get(19));
        listBanner.add(homeBirdBean);
        homeBirdBean = new HomeBirdBean(R.drawable.huamei4, R.drawable.huamei2, R.raw.huamei, listBird.get(20));
        listBanner.add(homeBirdBean);
        MyBannerAdapter bannerAdapter = new MyBannerAdapter(listBanner, getContext());
        mDataBinding.banner.setAdapter(bannerAdapter, true);//false不无限循环
        mDataBinding.banner.setBannerGalleryEffect(40, 40, 20, 1);
        mDataBinding.banner.isAutoLoop(true);//false不自动轮播
//        mDataBinding.banner.setIndicator(new RectangleIndicator(mContext));
        //点击监听
        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                showBirdInfo(listBanner.get(position));
            }
        });
    }

    private void showVolumeSetDialog() {
        VolumeSetDialog dialog = new VolumeSetDialog(mContext);
        dialog.show();
    }

    private void showBirdInfo(HomeBirdBean homeBirdBean) {
        BirdInfoDialog dialog = new BirdInfoDialog(mContext);
        dialog.setHomeBirdBean(homeBirdBean);
        dialog.show();
    }

}